<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Stone\Core;

/**
 * Description of Build
 *
 * @author Administrator
 */
class Build {
    //put your code here
    
    static public function checkDir() {
       
        if(!is_dir(APP_PATH)) {
            // 创建模块的目录结构
            self::buildAppDir(); 
        }else{
            self::checkAppDir();
        }
    
    }
    
    /**
     * 创建目录结构
     */
    static public function buildAppDir() {        
        if(!is_dir(APP_PATH)) mkdir(APP_PATH,0755,true);
        if(is_writeable(APP_PATH)) {
            $dirs   =   array(
                APP_PATH . "Config" . DIRECTORY_SEPARATOR,
                APP_PATH . "Model" . DIRECTORY_SEPARATOR,
                APP_PATH . "Controller" . DIRECTORY_SEPARATOR,
                APP_PATH .  "View" . DIRECTORY_SEPARATOR
            );
            
            foreach ($dirs as $dir){
                if(!is_dir($dir))  mkdir($dir,0755,true);
            }
            self::buildDirSecure($dirs);            
        }else{
            E('应用目录['.APP_PATH.']不可写，目录无法自动生成！<BR>请手动生成项目目录~');
        }
        
    }
    /**
     * 检查目录结构
     */
    static public function checkAppDir() {
        $dirs   =   array(
            APP_PATH . "Config" . DIRECTORY_SEPARATOR,
            APP_PATH . "Model" . DIRECTORY_SEPARATOR,
            APP_PATH . "Controller" . DIRECTORY_SEPARATOR,
            APP_PATH .  "View" . DIRECTORY_SEPARATOR
        );       
        foreach ($dirs as $dir){
            if(!is_dir($dir)){
                E('应用目录['.$dir.']不存在，请创建。~');
            }
        }
        
    }


    // 生成目录安全文件
    static public function buildDirSecure($dirs=array()) {
        // 目录安全写入（默认开启）
        defined('DIR_SECURE_FILENAME')  || define('DIR_SECURE_FILENAME',    'index.html');
        defined('DIR_SECURE_CONTENT')   || define('DIR_SECURE_CONTENT',     '');
        // 自动写入目录安全文件
        $content = DIR_SECURE_CONTENT;
        $files = explode(',', DIR_SECURE_FILENAME);
        foreach ($files as $filename){
            foreach ($dirs as $dir)
                file_put_contents($dir.$filename,$content);
        }
    }
    
}
